define(['jquery', 'bootstrap', 'utilback', 'table', 'form', 'myfileinput','zh','ztree'], function ($, undefined, Utilback, Table, Form,myfileInput,zh) {

	var Controller = {
		config: {
			get_dic: '/dic/read/detail',
			get_param: '/param/read/detail',
		},
		init: function() {

				// 初始化表格参数配置
				Table.api.init({
					singleSelect: true,
					extend: {
						index_url: './json/skate.json',
						add_url: 'shoesApplyFor.html',
						edit_url: 'shoesApplyFor.html',
						del_url: '/paramTable',
						multi_url: '/paramTable',
						guidIn_url: 'driverGuidIn.html', //添加导入页面
						audit_url:'serviceCheck.html',
	                    checkStart_url:'serviceCheckStart.html?biz=shoes',//发起审核
					}
				});
					var paramTable = $("#paramTable");
					// 初始化表格
					paramTable.bootstrapTable({
						toolbar: '#toolbar',
						url: $.fn.bootstrapTable.defaults.extend.index_url,
						method: 'get',
						columns: [
							[{
									field: 'id',
									title: 'ID'
								},
								{field: 'state',checkbox: true,'class':'hide'},
								{
									field: 'applyDate',
									title: '申请时间'
								},
								{
									field: 'dept',
									title: '部门'
								},
								{
									field: 'matter',
									title: '理由'
								},
								{
									field: 'count',
									title: '数量'
								},
								{
									field: 'operator',
									title: '申请人'
								},
								{
									field: 'pleaseBringPeople',
									title: '审批人'
								},
								{
									field: 'pleaseBringDate',
									title: '审批时间'
								},
								{
									field: 'audioStatus',
									title: '审核状态',
									events: Table.api.events.operate,formatter : function (value,row,index) {
										if(value == '审核中')
											return '<a href="' + $.fn.bootstrapTable.defaults.extend.audit_url + '" class="addtabsit" title="铁鞋申请审核">' + value + '</a>';
										return value;
								}},
								
								{
									field: 'operate',
									title: __('Operate'),
									events: Table.api.events.operate,
									formatter: function(value, row, index) {
										if(row.audioStatus != '待发起')
											return '';
										return Table.api.formatter.operate.call(this, value, row, index, paramTable);
									}
								}
							]
						]
					});
					// 为表格绑定事件
					Table.api.bindevent(paramTable);
					paramTable.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table fa.event.check', function () {
	                    var ids = Table.api.selectedids(paramTable);
	                    var audioStatus = Table.api.selectedFields(paramTable, 'audioStatus');
		                var options = paramTable.bootstrapTable('getOptions');
		                var toolbar = $(options.toolbar, paramTable);
	                    $('.btn-del').toggleClass('disabled', true);
	                    $('.btn-checkStart').toggleClass('disabled', true);
			            $('.btn-applyFor').toggleClass('disabled', true);
	                    if(audioStatus[0] == '待发起') {
	                    	$('.btn-del').toggleClass('disabled', false);
	                    	$('.btn-checkStart').toggleClass('disabled', false);
	                    } else if(audioStatus[0] == '审核中') {
		                   $('.btn-applyFor').toggleClass('disabled', false);
		                 }
	                });
					// 铁鞋申请审核
					$('.btn-applyFor').on('click',function(){
						var ids = Table.api.selectedids(paramTable);
						var options = paramTable.bootstrapTable('getOptions');
                        var url = options.extend.audit_url + (options.extend.audit_url.match(/(\?|&)+/)?"&id=" : "?id=") + ids.join(',') + '&t=A';
                        Util.api.open(url,__('铁鞋申请审核'));
					})

		            $('.btn-checkStart').on('click',function(){
		                var ids = Table.api.selectedids(paramTable);
		                var options = paramTable.bootstrapTable('getOptions');
		                var url = options.extend.checkStart_url + (options.extend.checkStart_url.match(/(\?|&)+/)?"&id=" : "?id=") + ids.join(',') + '&t=S';
		                Util.api.open(url,__('发起审核'));
		            });
		},
		edit: function() { // 初始化编辑页面
			// 导入的时间绑定
			Form.api.bindevent($("form[role=form]"));
            Util.api.initSelectTree({
                checkParent: true,
                parentEle: 'department',
                url: './json/list.json',
                data: JSON.stringify({keyword:''}),
                resetData: function(data) {
                   for(var i=0;i<data.length;i++) {
                       if(data[i].leaf == 0) {
                           data[i]['open'] = true;
                       }
                       data[i]['pId'] = data[i].parentId;
                       data[i]['name'] = data[i].deptName;
                   }
                   return data;
                }
            });
            var id = Util.api.query('id');
            var t = Util.api.query('t');
            if(id && t === 'U') {
                Form.api.fill($('#edit-form'), Controller.config.get_url, id, function(data) {
                });
            }
		},
		audit: function() {
			var id = Util.api.query('id');
	        var t = Util.api.query('t');
	        if(id && t === 'U') {
	            Form.api.fill($('#edit-form'), Controller.config.get_url, id, function(data) {
	            });
	        }
			var applyTable = $("#applyTable");
					// 初始化表格
					applyTable.bootstrapTable({
						url: './json/skate.json',
						method: 'get',
						columns: [
							[
								{
									field: 'auditPeople',
									title: '审核人'
								},
								{
									field: 'auditDate',
									title: '审核日期'
								},
								{
									field: 'remark',
									title: '备注'
								},
							]
						]
					});
					// 为表格绑定事件
					Table.api.bindevent(applyTable);
		}
	};
	return Controller;
});